<template>
  <div>
    <div class="flexrowbetween flexwrapper" style="align-items:flex-start">
      <div class="flex aligncenter">
        <div class="iconbox flexcenter commhover blackbg mr_10" @click="resetQuery">
            <i class="el-icon-refresh f-20" :class="{'rotate':isrefresh}"></i>
        </div>
        <el-button size="mini" icon="el-icon-plus" plain type="primary" @click="handleAdd">添加</el-button>
        <el-button size="mini" icon="el-icon-delete" class="mr_10" type="danger" :disabled='multiple' @click="handleDetele">删除</el-button>
      </div>
      <div>
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="0"
        >
          <el-form-item prop="keyword">
            <el-input class="searchinput"
              v-model="queryParams.keyword"
              placeholder="名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!-- <el-form-item class="mr_0">
            <el-dropdown trigger="click">
                <el-button size="mini" class="nor_border plr_9">
                  <svg-icon class-name="f-14" icon-class="listicon" />
                  <i class="el-icon-caret-bottom el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                    <div class="dropbody">
                      <el-checkbox-group v-model="checkedOption" class="flexcolumn" @change="changeSelect">
                        <el-checkbox v-for="(item,index) in selectoptions" :label="item.label" :key="index" class="m_5">{{item.label}}</el-checkbox>
                      </el-checkbox-group>
                    </div>
                </el-dropdown-menu>
            </el-dropdown>
          </el-form-item> -->
          <!-- <el-form-item>
            <el-dropdown trigger="click" @command="handleCommand">
                <el-button size="mini" class="nol_border plr_9">
                  <svg-icon class-name="f-14" icon-class="export" />
                  <i class="el-icon-caret-bottom el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item,index) in exportlist" :key="index" :command="item.id">{{item.name}}</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
          </el-form-item> -->
          <el-form-item>
            <el-button class="f-14"
              type=""
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            ></el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
        v-loading="loading"
        :data="tableData"
        border
        id="dragTable"
        ref="tablelayout"
        class="commtable"
        @selection-change="handleSelectionChange"
        row-key="id"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column v-if="qudaoOption.includes('ID')" label="ID" prop="id" width="80" align="center"/>
        <!-- <el-table-column v-if="qudaoOption.includes('类型')" label="类型" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.type }} </span>
          </template>
        </el-table-column> -->
        <el-table-column v-if="qudaoOption.includes('名称')" label="名称" prop="name" align="center"/>
        <el-table-column v-if="qudaoOption.includes('排序')" label="排序" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.level}}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="qudaoOption.includes('状态')" label="昵称" align="center">
          <template slot-scope="scope">
            <el-tag type="success" class="tag_success" v-if="scope.row.status==1">正常</el-tag>
            <el-tag type="danger" v-else class="tag_danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <div class="flex btnflex">
              <div class="commhover btn_green mr_10 c_fff" @click="handlePx(scope.row)">
                <i class="el-icon-sort f-14"></i>排序
              </div>
              <div class="commhover btn_yellow mr_10 c_fff" @click="handleEdit(scope.row)">
                <i class="el-icon-edit-outline f-14"></i>编辑
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.num"
        @pagination="getList"
      />

    <el-dialog append-to-body
      :title="title"
      :visible.sync="open"
      width="560px"
      class="dialogheaderbg"
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="100px" label-position="left">
        <el-form-item label="名称:" prop="name" v-if="type==1||type==2">
          <el-input v-model="form.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="排序:" prop="level" v-if="type==1||type==3">
          <el-input v-model="form.level" placeholder="排序值（0-10000）" min="0" max="10000" type="number"></el-input>
        </el-form-item>
        <el-form-item label="状态:" prop="status" v-if="type==1||type==2">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapState } from 'vuex';
import { ditchIndex,ditchAdd, ditchEdit,ditchLevelEdit,ditchDel } from '@/api/member'
import Sortable from 'sortablejs'
export default {
  data() {
    return {
      queryParams: {
        page: 1,
        num: 10,
        keyword: undefined,
      },
      total:1,
      loading:false,
      tableData:[],
      checkedOption:[],
      isrefresh:false,
      selectoptions:[
        {id:1,label:"ID"},
        {id:3,label:"名称"},
        {id:4,label:"排序"},
        {id:5,label:"状态"},
      ],
      open:false,
      form:{
        name:'',
        level:'',
        status:1,
      },
      rules:{
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        level:[{ required: true, message: "请输入排序值", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      type:1,
      title:'',
      multiple:true,
      rowids:[]
    };
  },

  computed:{
    ...mapState({
      qudaoOption:state => state.paramsoption.qudaoOption,
    }),
  },

  created(){
    let qudaoOption = JSON.parse(localStorage.getItem('qudaoOption'));
    if(qudaoOption){
      this.changeSelect(qudaoOption);
    }
    this.checkedOption=this.qudaoOption;
    this.getList()
  },

  methods: {

    getList() {
      this.loading = true;
      let params = { 
        page:this.queryParams.page,
        num: this.queryParams.num
      }
      if(this.queryParams.keyword){
        params.keyword = this.queryParams.keyword
      }
      ditchIndex(params).then((response) => {
        this.tableData = response.data.list;
        this.total = response.data.count;
        this.loading = false;
      });
      this.isrefresh = false
      this.loading = false;
    },

    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },

    resetQuery() {
      this.isrefresh = true
      this.resetForm("queryForm");
      this.handleQuery();
    },

    changeSelect(arr){
      this.$store.commit('SET_ACTIVEOPTION',arr);
      this.$nextTick(() => { //在数据加载完，重新渲染表格
        this.$refs.tablelayout&&this.$refs.tablelayout.doLayout();
      })
    },

    // 复选框
    handleSelectionChange(rows){
        this.rowids = rows.map((item) => item.id);
        this.multiple = !rows.length
    },

    handleCommand(command) {
      console.log(command);
    },

    handleDelete() {
      const userIds = this.rowids;
      this.$modal.confirm('是否确认删除选中的用户数据项？').then(function() {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    rowDrop () {
      const tbody = document.querySelector('#dragTable tbody')
      if(tbody){
        Sortable.create(tbody, {
          onEnd: ({ newIndex, oldIndex }) => {
            const currRow = this.tableData.splice(oldIndex, 1)[0]
            this.tableData.splice(newIndex, 0, currRow)
          }
        })
      }
    },

    handleAdd(){
      this.type = 1
      this.title = '添加'
      this.form = {
        name:'',
        status:1,
        level:""
      }
      this.resetForm('form')
      this.open = true
    },

    handleClose(){
      this.open = false
    },

    // handleSord(){

    // },
    handleDetele(){
      this.$confirm('确定删除选中的数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ditchDel({
          id:this.rowids
        }).then(()=>{
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
      }).catch(() => {});
    },

    submit(){
      this.$refs.form.validate(valid=>{
        if(!valid) return
        if (this.type == 2) {
          ditchEdit(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else if (this.type == 1) {
          ditchAdd(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        } else if (this.type == 3) {
          ditchLevelEdit({
            id:this.form.id,
            level: this.form.level
          }).then(response => {
            this.$modal.msgSuccess("修改成功成功");
            this.open = false;
            this.getList();
          });
        }
      })
    },

    handleEdit(row){
      this.type = 2
      this.form.id = row.id
      this.form.name = row.name
      this.form.status = row.status
      this.title = '编辑'
      this.open = true
    },

    handlePx(row){
      this.type = 3
      this.form.id = row.id
      this.form.level = row.level
      this.title = '排序'
      this.open = true
    }
    
  },
};
</script>

<style lang="scss" scoped>
.iconbox {
  width: 32px;
  height: 26px;
}
.imgmini{
    width: 40px;
    height: 40px;
    margin: auto;
}
.imgbox{
    width: 120px;
    height: 120px;
}
</style>